package com.meihua.baoleyi.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.bean.BankDataBean;
import com.meihua.baoleyi.ui.BankActivity;

import java.util.List;


/**
 * Created by Administrator on 2017/7/11.
 */

public class BankAdapter extends BaseAdapter {

    private BankActivity activity;
    private List<BankDataBean.DataBean> mBankList;
    private int type = 0;

    public BankAdapter(BankActivity activity, List<BankDataBean.DataBean> bankList, int type) {
        this.activity = activity;
        this.mBankList = bankList;
        this.type = type;
    }

    @Override
    public int getCount() {
        if (type == 2) {
            return mBankList == null ? 0 : mBankList.size();
        }
        return mBankList == null ? 1 : mBankList.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHandler h;
        if (view == null) {
            h = new ViewHandler();
            view = LayoutInflater.from(activity).inflate(R.layout.item_bank_list, null);
            h.bank_list_addbank_ll = (LinearLayout) view.findViewById(R.id.bank_list_addbank_ll);
            h.bank_list_bank_ll = (LinearLayout) view.findViewById(R.id.bank_list_bank_ll);
            h.iv_round = view.findViewById(R.id.iv_round);
            h.tv_banks_type = view.findViewById(R.id.tv_banks_type);
            h.tv_card_end_num = view.findViewById(R.id.tv_card_end_num);

            view.setTag(h);
        } else {
            h = (ViewHandler) view.getTag();
        }
        if (mBankList == null || mBankList.size() == i) {
            h.bank_list_addbank_ll.setVisibility(View.VISIBLE);
            h.bank_list_bank_ll.setVisibility(View.GONE);
        } else {
            h.bank_list_addbank_ll.setVisibility(View.GONE);
            h.bank_list_bank_ll.setVisibility(View.VISIBLE);
            if (i % 2 == 0) {
                h.bank_list_bank_ll.setBackgroundResource(R.drawable.bank_item_bg_0);
            } else {
                h.bank_list_bank_ll.setBackgroundResource(R.drawable.bank_item_bg_1);
            }
            h.tv_banks_type.setText(mBankList.get(i).getBanks_name());
            Glide.with(activity).load("").into(h.iv_round);
            String bankCard = mBankList.get(i).getBank_card();
            if (bankCard.length() > 4)
                bankCard = bankCard.substring(bankCard.length() - 4);
            h.tv_card_end_num.setText(bankCard);
        }

        return view;
    }


    class ViewHandler {
        public LinearLayout bank_list_addbank_ll;//添加银行卡
        public LinearLayout bank_list_bank_ll;//银行卡
        public ImageView iv_round;
        public TextView tv_banks_type;
        public TextView tv_card_end_num;
    }
}
